package cn.caplike.demo.netty.heartbeat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * Details: 启动之前的 MyChatClient 观察控制台打印
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2021-11-27 21:06
 */
public class MyServer {

    public static void main(String[] args) throws InterruptedException {
        // @formatter:off
        final EventLoopGroup parentGroup = new NioEventLoopGroup();
        final EventLoopGroup  childGroup = new NioEventLoopGroup();

        try {
            final ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(parentGroup,  childGroup)
                           .channel(NioServerSocketChannel.class)
                           .handler(new LoggingHandler(LogLevel.INFO)) // 处理 parentGroup, 真正的连接请求之后, 会由 handler 处理, 仅仅处理连接本身
                           .childHandler(new MyServerChannelInitializer()); // 连接真正的事件处理

            final ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
    }
}
